import { Component, OnInit } from '@angular/core';
import { SocketService } from './../../services/socket.service';
import { NetService } from './../../services/net.service';
declare var AMap: any;
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  public recordingList = [];
  public headers = ['区域', '小区名称', '预（现）售楼幢号', '均价', '意向登记时间', '现场接受材料地点'];
  public area = '';
  public yxdjState = '2';
  public title = '';
  public updateTime = '';

  public checkInOptions = [
    { label: '全部', value: '' },
    { label: '未开始登记', value: '1' },
    { label: '正在登记', value: '2' },
    { label: '已登记', value: '3'},
  ];
  public ereaOptions = [
    { label: '全部', value: '' },
    { label: '城6区', value: 'c6'},
    { label: '长安区', value: 'ca'}
  ];

  constructor(
    private socketService: SocketService,
    private net: NetService
  ) { }

  // get clientCounts() {
  //   return this.socketService.clientCounts;
  // }

  changeCond() {
    this.getHouseList();
  }

  changeSearch() {
    this.area = '';
    this.yxdjState = '';
    this.getHouseList();
  }

  getPrice(prices) {
    const sum = prices.reduce((cur, next) => Number(cur) + Number(next));
    return (sum / prices.length).toFixed(0);
  }


  async getHouseList() {
    let url = '/api/houses?pageCount=Infinity';
    if (this.title) {
      url += `&title=${this.title}`;
    }
    if (this.area) {
      url += `&area=${this.area}`;
    }
    if (this.yxdjState) {
      url += `&yxdjState=${this.yxdjState}`;
    }
    const result = await this.net.get(url);
    if (result) {
      this.recordingList = result['data']['list'];
    }
  }

  async ngOnInit() {
    await this.getHouseList();
    this.updateTime = this.recordingList[0].updatedAt;
  }

}
